package com.wyy.service.impl;

import com.wyy.mapper.OrderMapper;
import com.wyy.pojo.AppreciatesOrder;
import com.wyy.pojo.model.UserToken;
import com.wyy.service.OrderService;
import com.wyy.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @program: gytcb
 * @author: 吴磊
 * @create: 2021-06-26 14:40
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 查看订单状态
     *
     * @param appreciatesOrder
     * @return
     */
    @Override
    public boolean checkOrderStatus(AppreciatesOrder appreciatesOrder) {
        Integer i = orderMapper.getOrderStatusByOrderId(appreciatesOrder.getOrderId());
        if (i == null || i == 0) {
            return false;
        }
        return true;
    }

    /**
     * 插入订单
     *
     * @param appreciatesOrder
     * @return
     */
    @Override
    public Integer insertOrder(AppreciatesOrder appreciatesOrder) {
        String isLoginToken = tokenUtils.parseTokenToken();
        if (StringUtils.isEmpty(isLoginToken)) {
            //用户没有登录，匿名赞赏
            appreciatesOrder.setOrderBuyerId("");
            appreciatesOrder.setOrderBuyerName("匿名");
        } else {
            //用户已经登录
            UserToken loginUser = tokenUtils.getInfoFromToken(isLoginToken);
            appreciatesOrder.setOrderBuyerId(loginUser.getUserId());
            appreciatesOrder.setOrderBuyerName(loginUser.getUserNickname());
        }
        Integer integer = orderMapper.insertOrder(appreciatesOrder);
        return integer;
    }

    /**
     * 更新订单状态
     *
     * @param appreciatesOrder
     * @return
     */
    @Override
    public Integer updateOrder(AppreciatesOrder appreciatesOrder) {
        Integer integer = orderMapper.updateOrder(appreciatesOrder);
        return integer;
    }
}
